package com.cskaoyan.controller.admin;

import com.cskaoyan.bean.MarketGoods;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.goods.vo.CatAndBrandVo;
import com.cskaoyan.bean.goods.bo.CreateGoodsBo;
import com.cskaoyan.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("admin/goods")
public class GoodsController {
    @Autowired
    GoodsService goodsService;

    @RequestMapping("list")
    public BaseRespVo list(BasePageInfo basePageInfo,String name,Integer goodsId,String goodsSn) {
        CommonData<MarketGoods> baseRespVoCommonData = goodsService.queryAllGoods(basePageInfo,name,goodsId,goodsSn);
        BaseRespVo baseRespVo = BaseRespVo.ok(baseRespVoCommonData);
        baseRespVo.setErrmsg("成功");
        return baseRespVo;
    }

    @RequestMapping("catAndBrand")
    public BaseRespVo catAndBrand() {
        CatAndBrandVo catAndBrandVo = goodsService.queryCatAndBrand();
        BaseRespVo baseRespVo = BaseRespVo.ok(catAndBrandVo);
        baseRespVo.setErrmsg("成功");
        return baseRespVo;

    }

    @RequestMapping("create")
    public BaseRespVo create(@RequestBody CreateGoodsBo createGoodsBo) {

        int code = goodsService.createGoods(createGoodsBo);
        BaseRespVo<Object> baseRespVo = new BaseRespVo<>();
        if (code == 1) {
            baseRespVo.setErrmsg("成功");
            return baseRespVo;
        }
        baseRespVo.setErrmsg("失败");
        baseRespVo.setErrno(402);
        return baseRespVo;
    }

    @RequestMapping("update")
    public BaseRespVo update(@RequestBody CreateGoodsBo createGoodsBo){
        int code = goodsService.updateGoods(createGoodsBo);
        BaseRespVo<Object> baseRespVo = new BaseRespVo<>();
        if(code==1){
            baseRespVo.setErrmsg("成功");
            return baseRespVo;
        }
        baseRespVo.setErrmsg("失败");
        return baseRespVo;
    }

    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody MarketGoods marketGoods){
        int code=goodsService.deleteGoods(marketGoods);
        BaseRespVo<Object> baseRespVo = new BaseRespVo<>();
        if(code==1){
            baseRespVo.setErrmsg("成功");
            return baseRespVo;
        }
        baseRespVo.setErrmsg("失败");
        return baseRespVo;
    }

    @RequestMapping("detail")
    public BaseRespVo detail(Integer id){
        CreateGoodsBo createGoodsBo= goodsService.showAllDetails(id);
        BaseRespVo baseRespVo = BaseRespVo.ok(createGoodsBo);
        baseRespVo.setErrmsg("成功");
        return baseRespVo;
    }

}

